package controller

import (
	"fmt"
	"github.com/gin-gonic/gin"
	"go_batch_create_orders/admin/model"
	"go_batch_create_orders/admin/service"
	"go_batch_create_orders/admin/validation"
	"go_batch_create_orders/common"
	"go_batch_create_orders/package/excel"
	"go_batch_create_orders/utils"
)

func AddUser(ctx *gin.Context) {
	user := new(model.Users)
	err := ctx.ShouldBind(&user)

	data := utils.JsonInit()
	data.SetMessage("添加成功")
	if err != nil {
		data.SetCode(502)
		data.SetMessage("添加失败")
		ctx.JSON(502, data)
	}
	s := new(service.UserService)
	res := s.AddUser(user)
	if res > 0 {
		data.SetCode(200)
		data.SetMessage("添加成功")
		ctx.JSON(200, data)
	} else {
		data.SetCode(502)
		data.SetMessage("添加失败")
		ctx.JSON(502, data)
	}
}

func DeleteUser(ctx *gin.Context) {
	s := new(service.UserService)
	s.DeleteUser(3)
}

func UpdateUser(ctx *gin.Context) {
	//_, header, err := ctx.Request.FormFile("file")
	//s := new(service.UploadService)
	//s.Resource = header
	//suffix := make([]string,0,5)
	//suffix = append(suffix,"xlsx","jpg","jpeg")
	//s.Suffix = suffix
	//s.Path = "./excelLog/"
	//s.NewFileName = strconv.FormatInt(time.Now().UnixNano(),10) + path.Ext(header.Filename)
	//s.Context = ctx
	//filePath, err := s.Upload()
	filePath := "./excelLog/1595236219539900500.xlsx"
	excelInfo, err := excel.ImportExcel(filePath)
	fmt.Println(excelInfo)
	fmt.Println(err)
	return
	new(service.UserService).UpdateUser()

}

func GetToken(ctx *gin.Context) {
	data := utils.JsonInit()
	data.SetCode(200)
	data.SetMessage("请求成功")
	users := model.Users{Id: 1, UserName: "张三"}
	key, err := utils.GenerateSecretKey(users)
	if err != nil {
		data.SetCode(500)
		data.SetMessage("登录失败")
		ctx.JSON(200, data)
	}
	data.SetData(key)
	ctx.JSON(200, data)

}

func Login(ctx *gin.Context) {
	loginRequest := model.UserLogin{}
	err := ctx.ShouldBindJSON(&loginRequest)
	fmt.Println(loginRequest)
	if err != nil {
		common.JsonError("参数有误", ctx)
		return
	}
	userValidate := new(validation.UserValidation)
	err = userValidate.ValidateUserLogin(loginRequest)
	if err != nil {
		common.JsonError(err.Error(), ctx)
		return
	}
	userService := new(service.UserService)
	login, err := userService.Login(&loginRequest)
	if err != nil {
		common.JsonError(err.Error(), ctx)
		return
	}
	token, err := utils.GenerateSecretKey(login)
	if err != nil {
		common.JsonError("登录失败", ctx)
		return
	}
	common.JsonSuccess(token, "获取成功", ctx)

	//userInfo, exists := ctx.Get("userInfo")
	//users := userInfo.(*model.Users) //传输过来需要重新进行类型断言 取值操作
	//if exists == true {
	//	fmt.Println(users.UserName)
	//}
}

func Test(ctx *gin.Context) {

	data := utils.JsonInit()
	data.SetCode(200)
	data.SetMessage("请求成功")
	users := model.Users{
		UserName: "test",
		Id:       80,
		Sex:      1,
	}
	data.SetData(users)
	ctx.JSON(200, data)

	v := new(validation.UserValidation)
	err := v.Validate(users)
	//err := utils.ValidateOneField(10, utils.Min(5,"最小值5"),utils.Required("id 必填"),utils.Max(9,"最大不超过9"))
	fmt.Println(err.Error())
}
